﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Ninject;
using System.Globalization;
using System.Web.Http.Dispatcher;

namespace Wicresoft.EKA.MVCExtensions
{
    public class NInjectHttpControllerActivator : IHttpControllerActivator, IRegister
    {
        public IKernel Kernel { get; private set; }

        public NInjectHttpControllerActivator()
        {
            this.Kernel = new StandardKernel();
        }

        public void Register<TFrom, TTo>() where TTo : TFrom
        {
            this.Kernel.Bind<TFrom>().To<TTo>();
        }

        public System.Web.Http.Controllers.IHttpController Create(System.Net.Http.HttpRequestMessage request, System.Web.Http.Controllers.HttpControllerDescriptor controllerDescriptor, Type controllerType)
        {
            if (controllerType == null)
            {
                throw new System.Web.HttpException(404,
                                            "DefaultControllerFactory_NoControllerFound");
            }
            if (!typeof(System.Web.Http.Controllers.IHttpController).IsAssignableFrom(controllerType))
            {
                throw new ArgumentException(
                        "DefaultControllerFactory_TypeDoesNotSubclassControllerBase",
                    "controllerType");
            }
            return (System.Web.Http.Controllers.IHttpController)this.Kernel.TryGet(controllerType);
        }
    }
}
